package com.Txunda.fxw.ui.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.login.LoginAty;
import com.Txunda.fxw.ui.adapter.VideoFeedAdapter;
import com.Txunda.fxw.ui.activity.other.LeaveMessageAty;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.toolkit.config.Config;
import com.Txunda.fxw.ui.http.FeedBack;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * ====================================
 * 作者：DUKE_HwangZj
 * 日期：2018/11/14
 * 时间：22:08
 * 描述：视频播放
 * 修改：
 * 说明:
 * ====================================
 */
public class VideoPlayAty extends BaseAty {

    @BindView(R.id.play_top_layout)
    FrameLayout playTopLayout;
    @BindView(R.id.evaluate_tv)
    TextView evaluateTv;
    @BindView(R.id.evaluate_lv)
    PullToRefreshListView evaluateLv;
    @BindView(R.id.custom_video_player_standard)
    JzvdStd customVideoPlayerStandard;

    private String url;
    private String title;
    private String com_id;

    private FeedBack feedBack;
    private int p = 1;

    private VideoFeedAdapter videoFeedAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customVideoPlayerStandard.setUp(url, title, Jzvd.SCREEN_WINDOW_NORMAL);
        customVideoPlayerStandard.backButton.setImageResource(R.drawable.icon_back_white);
        customVideoPlayerStandard.backButton.setVisibility(View.VISIBLE);
        customVideoPlayerStandard.backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        customVideoPlayerStandard.startVideo();

        evaluateLv.setAdapter(videoFeedAdapter);

        evaluateLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                p = 1;
                getEvaluate();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                p++;
                getEvaluate();
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_video_play;
    }

    @Override
    protected void initialized() {
        feedBack = new FeedBack();
        com_id = getIntent().getStringExtra("com_id");
        url = getIntent().getStringExtra("url");
        Log.d("zzzzzzzzz", "initialized: ==========="+url);
        title = getIntent().getStringExtra("title");
        videoFeedAdapter = new VideoFeedAdapter(this);
    }

    @Override
    protected void requestData() {
        getEvaluate();
    }

    private void getEvaluate() {
        feedBack.feedbackList(p, "sy" + com_id, this);
    }

    @Override
    @OnClick({R.id.evaluate_tv})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.evaluate_tv:// 评价
                if (!Config.isLogin()) {
                    startActivity(LoginAty.class, null);
                    return;
                }
                showProgressDialog();
                String m_id = application.getUserInfo().get("m_id");
                feedBack.comment(m_id, this);
                break;
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("/Api/Feedback/comment")) {
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data))
                return;
            application.setUserInfo(data);

            Bundle bundle = new Bundle();
            bundle.putString("type", "sy" + com_id);
            startActivity(LeaveMessageAty.class, bundle);
            return;
        }
        if (requestUrl.contains("/Api/Feedback/FeedbackList")) {
            List<Map<String, String>> data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            if (1 == p)
                videoFeedAdapter.clear();
            if (!ListUtils.isEmpty(data))
                videoFeedAdapter.updata(data);
            evaluateLv.onRefreshComplete();
        }
    }

    @Override
    public void onError(String requestUrl, Map<String, String> error) {
        super.onError(requestUrl, error);
        evaluateLv.onRefreshComplete();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }
}
